package com.hongsen.cn.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableInt;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.hongsen.cn.Utils.LogUtil;
import com.hongsen.cn.Utils.RxBus;
import com.hongsen.cn.Utils.SPUtil;
import com.hongsen.cn.Utils.ToastUtils;
import com.hongsen.cn.adapter.WorkDescAdapter;
import com.hongsen.cn.base.BaseActivity;
import com.hongsen.cn.bean.BaseTitleBean;
import com.hongsen.cn.bean.OrderBean;
import com.hongsen.cn.bean.PoliceBean;
import com.hongsen.cn.bean.PoliceListBean;
import com.hongsen.cn.bean.RxValue;
import com.hongsen.cn.bean.TheKeyPersonelBean;
import com.hongsen.cn.bean.TitleBean;
import com.hongsen.cn.contract.PoliceDescView;
import com.hongsen.cn.dayandmonth.R;
import com.hongsen.cn.dayandmonth.databinding.ActivityPoliceDescBinding;
import com.hongsen.cn.presenter.PoliceDescPresenter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

import static com.hongsen.cn.bean.RxBusEventKey.BASE_TITLE_CLICK;

public class PoliceDescActivity extends BaseActivity<PoliceDescPresenter> implements PoliceDescView {

    public ActivityPoliceDescBinding binding;
    public PoliceListBean bean;
    public ObservableInt FBI_STATUE_CHECK = new ObservableInt(0);
    public ObservableBoolean FBI_VIEW_GONE = new ObservableBoolean(false);
    public WorkDescAdapter adapter;
    public List<OrderBean> list = new ArrayList<>();


    @Override
    protected PoliceDescPresenter createPresenter() {
        return new PoliceDescPresenter(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

        binding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.activity_police_desc, null, false);
        binding.setActivity(this);
        baseFl.addView(binding.getRoot());

        presenter = new PoliceDescPresenter(this);
        //presenter.getTitleInfo();

        TitleBean titleBean = TitleBean.getBean(PoliceDescActivity.this);
        if (titleBean != null) {
            LogUtil.e("民警详情发送title数据");
            RxBus.getDefault().post(new RxValue("module_FBI_WG", titleBean.getTitleOne()));
            RxBus.getDefault().post(new RxValue("module_FBI_JD", titleBean.getTitleTwo()));
            RxBus.getDefault().post(new RxValue("module_FBI_JD_CC", titleBean.getTitleThree()));
            RxBus.getDefault().post(new RxValue("module_FBI_DT", titleBean.getTitleFour()));
            RxBus.getDefault().post(new RxValue("module_FBI_CS", titleBean.getTitleFive()));
        }else{
            LogUtil.e("警员详情titleBean== null");

        }

        bean = getIntent().getParcelableExtra("module_bean");
        if (bean != null) {
            presenter.getPhotoStrs(bean.getMjxb());

            if (!TextUtils.isEmpty(bean.getMjxm())) {
                binding.tvName.setText(bean.getMjxm());
            }

            if (!TextUtils.isEmpty(bean.getJh())) {
                binding.tvPoliceNum.setText(bean.getJh());
            }

            //性别错、岗位空
            if (bean.getMjxb() != null) {
                switch (bean.getMjxb()) {
                    case "1":
                        binding.tvSex.setText("男");
                        break;
                    case "2":
                        binding.tvSex.setText("女");
                        break;
                    default:
                }
            }

            if (!TextUtils.isEmpty(bean.getMjgwmc())) {
                binding.tvPost.setText(bean.getMjgwmc());
            }

            if (bean.getMjgwmc() != null) {
                if (bean.getMjgwmc().equals("管教")) {
                    binding.clMiddle.setVisibility(View.VISIBLE);
                    binding.clRight.setVisibility(View.VISIBLE);

                    //只有管教显示工作进度
                    binding.llModuleLeftProgress.setVisibility(View.VISIBLE);
                    binding.llModuleRightCircle.setVisibility(View.VISIBLE);

                    FBI_VIEW_GONE.set(true);
                    binding.rbModuleYYCL.setVisibility(View.GONE);
                    binding.rbModuleEightRead.setVisibility(View.VISIBLE);
                    binding.rbModuleWG.setVisibility(View.VISIBLE);
                    binding.rbModuleYY.setVisibility(View.VISIBLE);
                    binding.rbModuleJSEyes.setVisibility(View.VISIBLE);
                    binding.rbModuleJDXJ.setVisibility(View.VISIBLE);

                    FBI_STATUE_CHECK.set(0);
                    binding.tvModuleTitleThree.setText("谈话类别");
                    presenter.getOrderList("谈话", "1", bean.getMjbh());

                } else if (bean.getMjgwmc().contains("所长")){

                    /**
                     * 关闭除了所领导的预约处理以外其他的
                     */
                    binding.rbModuleYYCL.setVisibility(View.VISIBLE);
                    binding.rbModuleEightRead.setVisibility(View.GONE);
                    binding.rbModuleWG.setVisibility(View.GONE);
                    binding.rbModuleYY.setVisibility(View.GONE);
                    binding.rbModuleJSEyes.setVisibility(View.GONE);
                    binding.rbModuleJDXJ.setVisibility(View.GONE);

                    binding.llModuleLeftProgress.setVisibility(View.GONE);
                    binding.llModuleRightCircle.setVisibility(View.GONE);

                    binding.clMiddle.setVisibility(View.VISIBLE);
                    binding.clRight.setVisibility(View.VISIBLE);

                    binding.tvModuleTitleThree.setText("预约时间");
                    presenter.getOrderList("预约", "2", bean.getMjbh());

                }else{
                    LogUtil.e("角色非管教-->" + bean.getMjgwmc());
                    binding.clMiddle.setVisibility(View.INVISIBLE);
                    binding.clRight.setVisibility(View.INVISIBLE);
                }
            }
        }else{
            LogUtil.e("policeDescActivity 传递Bean == null");
        }

        adapter = new WorkDescAdapter(this, list, "");
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(adapter);

        binding.btnBack.setOnClickListener(this);
        binding.rbModuleEightRead.setOnClickListener(this);
        binding.rbModuleWG.setOnClickListener(this);
        binding.rbModuleYY.setOnClickListener(this);
        binding.rbModuleJSEyes.setOnClickListener(this);
        binding.rbModuleJDXJ.setOnClickListener(this);
        binding.rbModuleYYCL.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        int sS_ID = v.getId();
        if (sS_ID == R.id.btnBack) {
            finish();
        } else if (sS_ID == R.id.rbModuleEightRead) {
            binding.tvModuleTitleThree.setText("谈话类别");
            FBI_STATUE_CHECK.set(0);
            presenter.getOrderList("谈话", "1", bean.getMjbh());
        } else if (sS_ID == R.id.rbModuleWG) {
            FBI_STATUE_CHECK.set(1);
            binding.tvModuleTitleThree.setText("违规行为");
            presenter.getOrderList("违规", "1", bean.getMjbh());
        } else if (sS_ID == R.id.rbModuleYY) {
            FBI_STATUE_CHECK.set(2);
            binding.tvModuleTitleThree.setText("预约时间");
            presenter.getOrderList("预约", "1", bean.getMjbh());
        } else if (sS_ID == R.id.rbModuleJSEyes) {
            FBI_STATUE_CHECK.set(3);
            binding.tvModuleTitleThree.setText("状态");
            presenter.getOrderList("监室耳目", "1", bean.getMjbh());
        } else if (sS_ID == R.id.rbModuleJDXJ) {
            FBI_STATUE_CHECK.set(4);
            binding.tvModuleTitleThree.setText("状态");
            presenter.getOrderList("加戴械具", "1", bean.getMjbh());
        } else if (sS_ID ==R.id.rbModuleYYCL){
            binding.tvModuleTitleThree.setText("预约时间");
            presenter.getOrderList("预约", "2", bean.getMjbh());
        }
    }

    @Override
    public void getPhotoStr(String imgStr) {
        try {
            binding.imgHead.setImageBitmap(getBitmap(imgStr));
        } catch (Exception e) {
            LogUtil.e("图片加载出错");
        }

        presenter.login(bean.getJh()+"00");
    }

    @Override
    public void getOrderList(String bodyId, String type, List<OrderBean> str) {
        if (str == null) {
            list.clear();
            adapter.notifyDataSetChanged();
            binding.recyclerView.setVisibility(View.INVISIBLE);
            binding.tvModuleNoInfo.setVisibility(View.VISIBLE);
            LogUtil.e("数据返回空");
        } else {
            if (str.size() != 0) {
                list.clear();
                list.addAll(str);
                adapter.notifyDataSetChanged();
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.tvModuleNoInfo.setVisibility(View.INVISIBLE);
            } else {
                binding.recyclerView.setVisibility(View.INVISIBLE);
                binding.tvModuleNoInfo.setVisibility(View.VISIBLE);
            }
        }
    }

    public Bitmap getBitmap(String str) {
        byte[] decode = Base64.decode(str, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
        return bitmap;
    }

    @Override
    public void getTitleInfo(BaseTitleBean str) {
        RxBus.getDefault().post(new RxValue("module_FBI_WG", String.valueOf(str.getEffectiveVO().getNissinMonthlyCount())));
        RxBus.getDefault().post(new RxValue("module_FBI_JD", String.valueOf(str.getMechanicalDeviceVo().getNissinMonthlyCount())));
        RxBus.getDefault().post(new RxValue("module_FBI_JD_CC", String.valueOf(str.getMechanicalDeviceVo().getArePersonnelCount())));
        RxBus.getDefault().post(new RxValue("module_FBI_DT", String.valueOf(str.getRiskVO().getNissinMonthlyCount())));
        RxBus.getDefault().post(new RxValue("module_FBI_CS", String.valueOf(str.getOutVO().getNissinMonthlyCount())));
    }

    @Override
    public void getLogiInfo(PoliceBean bean) {

        if (bean.getPercentage() != null) {
            binding.picCharDay.setMaxValue(100);
            binding.picCharDay.setValue(Float.valueOf(bean.getPercentage())*100);

            binding.picCharMonth.setMaxValue(100);
            binding.picCharMonth.setValue(Float.valueOf(bean.getPercentage())*100);
        } else {
            LogUtil.e(bean.getMjxm() + "工作进度空");
        }
    }
}
